package com.ellen.mac.chatdemo.handler;


import android.content.Context;

import com.avos.avoscloud.im.v2.AVIMClient;
import com.avos.avoscloud.im.v2.AVIMMessage;
import com.avos.avoscloud.im.v2.AVIMConversation;
import com.avos.avoscloud.im.v2.AVIMConversationEventHandler;
import com.ellen.mac.chatdemo.cache.ACache;
import com.ellen.mac.chatdemo.cache.ConversationItemCache;
import com.ellen.mac.chatdemo.event.ConversationReadStatusEvent;
import com.ellen.mac.chatdemo.event.MessageUpdatedEvent;
import com.ellen.mac.chatdemo.event.OfflineMessageCountChangeEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;


/**
 * Created by mac on 2017/12/2.
 * 和 Conversation 相关的事件的 handler
 * 需要应用主动调用  AVIMMessageManager.setConversationEventHandler
 * 关于回调会何时执行可以参见 https://leancloud.cn/docs/realtime_guide-android.html#添加其他成员
 */
public class ConversationHandler extends AVIMConversationEventHandler {

    private static ConversationHandler eventHandler;
    ACache mACache;
    public static synchronized ConversationHandler getInstance(Context context) {
        if (null == eventHandler) {
            eventHandler = new ConversationHandler(context);

        }
        return eventHandler;
    }

    private ConversationHandler(Context context ){
        mACache = ACache.get(context);
    }

    @Override
    public void onUnreadMessagesCountUpdated(AVIMClient client, AVIMConversation conversation) {
        ConversationItemCache.getInstance().insertConversation(conversation.getConversationId());
        //todo Cache
        mACache.put(conversation.getConversationId(),conversation.getConversationId());
        EventBus.getDefault().post(new OfflineMessageCountChangeEvent());
    }

    @Override
    public void onLastDeliveredAtUpdated(AVIMClient client, AVIMConversation conversation) {
        ConversationReadStatusEvent event = new ConversationReadStatusEvent();
        event.conversationId = conversation.getConversationId();
        EventBus.getDefault().post(event);
    }

    @Override
    public void onLastReadAtUpdated(AVIMClient client, AVIMConversation conversation) {
        ConversationReadStatusEvent event = new ConversationReadStatusEvent();
        event.conversationId = conversation.getConversationId();
        EventBus.getDefault().post(event);
    }

    @Override
    public void onMemberLeft(AVIMClient client, AVIMConversation conversation, List<String> members, String kickedBy) {
        // 因为不同用户需求不同，此处暂不做默认处理，如有需要，用户可以通过自定义 Handler 实现
    }

    @Override
    public void onMemberJoined(AVIMClient client, AVIMConversation conversation, List<String> members, String invitedBy) {
    }

    @Override
    public void onKicked(AVIMClient client, AVIMConversation conversation, String kickedBy) {
    }

    @Override
    public void onInvited(AVIMClient client, AVIMConversation conversation, String operator) {
    }

    @Override
    public void onMessageRecalled(AVIMClient client, AVIMConversation conversation, AVIMMessage message) {
        EventBus.getDefault().post(new MessageUpdatedEvent(message));
    }

    @Override
    public void onMessageUpdated(AVIMClient client, AVIMConversation conversation, AVIMMessage message) {
        EventBus.getDefault().post(new MessageUpdatedEvent(message));
    }

}
